//
//  SleepAnalysisView.swift
//  DaRings
//
//  Created by 尹琼 on 2022/11/3.
//

import UIKit
import MYModule
import RxCocoa
import RxSwift

class SleepAnalysisCell: SleepBaseCell {
    
    let titleLabel = Label()
    
    let efficiencyView = SleepAnalysisSubView()
    
    let latencyView = SleepAnalysisSubView()
    
    let timingView = SleepAnalysisSubView()
    
    let stackView = StackView()
    
    
    override func makeUI() {
        super.makeUI()
        
        titleLabel.font = Fonts.boldSystemFont(ofSize: 14)
        titleLabel.textColor = UIColor.assist_222222
        titleLabel.text = "sleep_analysis".language
        
        stackView.axis = .vertical
        stackView.distribution = .equalSpacing
        stackView.alignment = .leading
        stackView.spacing = 20
        
        stackView.addArrangedSubview(efficiencyView)
        stackView.addArrangedSubview(latencyView)
        stackView.addArrangedSubview(timingView)
        
        bgView.addSubview(titleLabel)
        bgView.addSubview(stackView)
        
        titleLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(20)
            make.top.equalToSuperview().offset(14)
        }
        
        efficiencyView.snp.makeConstraints { make in
            make.right.equalToSuperview()
        }
        latencyView.snp.makeConstraints { make in
            make.right.equalToSuperview()
        }
        timingView.snp.makeConstraints { make in
            make.right.equalToSuperview()
        }
        
        stackView.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview()
            make.top.equalTo(titleLabel.snp.bottom).offset(20)
            make.bottom.equalToSuperview().offset(-20)
        }
        
    }
    
    override func bind(to cellType: SleepViewModel.CellType) {
        super.bind(to: cellType)
        
        guard let viewModel = cellType.viewModel as? SleepAnalysisViewModel else { return }
        
        let disposeBag = DisposeBag()
        
        viewModel.efficiency.subscribe(onNext: {[weak self] in
            
            self?.efficiencyView.model = $0
            
        }).disposed(by: disposeBag)
        
        viewModel.latency.subscribe(onNext: {[weak self] in
            
            self?.latencyView.model = $0
            
        }).disposed(by: disposeBag)
        
        viewModel.timing.subscribe(onNext: {[weak self] in
            
            self?.timingView.model = $0
            
        }).disposed(by: disposeBag)
        
        
    }
    

}

// MARK: - SleepAnalysisSubCell
extension SleepAnalysisCell {
    class SleepAnalysisSubView: View {
        
        var model: SleepAnalysisModel? {
            
            didSet {
                
                guard let model = model else {
                    return
                }

                iconImageView.image = UIImage(named: model.iconName)
                titleLabel.text = model.title
                detaiLabel.attributedText = model.detailAttritedText
                rightDataLabel.attributedText = model.data.attritedText
                
            }
            
        }
        
        let iconImageView = ImageView()
        
        let titleLabel = Label()
        
        let detaiLabel = Label()
        
        let rightDataLabel = Label()
        
        
        override func makeUI() {
            super.makeUI()
            
            
            titleLabel.font = Fonts.systemFont(ofSize: 14)
            titleLabel.textColor = UIColor.assist_222222
            
            detaiLabel.numberOfLines = 0
            
            addSubview(iconImageView)
            addSubview(titleLabel)
            addSubview(detaiLabel)
            addSubview(rightDataLabel)
            
            iconImageView.snp.makeConstraints { make in
                make.left.equalToSuperview().offset(20)
                make.centerY.equalToSuperview()
                make.size.equalTo(40)
            }
            
            titleLabel.snp.makeConstraints { make in
                make.top.equalToSuperview()
                make.left.equalTo(iconImageView.snp.right).offset(20)
            }
            
            detaiLabel.snp.makeConstraints { make in
                make.left.equalTo(titleLabel.snp.left)
                make.right.lessThanOrEqualToSuperview().offset(-20)
                make.top.equalTo(titleLabel.snp.bottom).offset(6)
                make.bottom.equalToSuperview()
            }
            
            rightDataLabel.snp.makeConstraints { make in
                make.right.equalToSuperview().offset(-20)
                make.centerY.equalTo(titleLabel.snp.centerY)
            }
            
            
        }
        
    }
    
}




